package com.example.demo;

import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/login")
public class loginController {
    @RequestMapping("/check")
    public boolean check(String username, String password, HttpSession session) {
        //对账号密码进行校验，Spring提供的方法，保证userName和password都不为空且长度不为零
        if(!StringUtils.hasLength(username) && !StringUtils.hasLength(password)) {
            return false;
        }
        if(!"xxwu".equals(username) || !"1123xy".equals(password)) {
            return false;
        }
        //将对应字符串session的值设置为username的值
        session.setAttribute("username", username);
        return true;
    }
    @RequestMapping("/getLoginInfo")
    public String getLoginInfo(HttpSession session) {
        String username = (String) session.getAttribute("username");
        if(StringUtils.hasLength(username)) {
            return username;
        } else {
            return null;
        }
    }
}
